package uiLayer;

import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;

import modelLayer.Schedule;
import modelLayer.Class;
import controlLayer.ClassCtr;
import controlLayer.ScheduleCtr;
import uiLayer.util.MainPanelTemplate;
import utilLayer.AlreadyExistsException;

	

/**
 * SchedulePanel is part of the GUI layer of the Mentiqa Skoleadministrationssystem.
 * SchedulePanel registers user input regarding schedules.
 *
 */
public class SchedulePanel extends MainPanelTemplate {
	
	private ScheduleCtr scheduleCtr;
	private JList<Schedule> scheduleList;
	private DefaultListModel<Schedule> scheduleListModel;
	private int numberOfBlocks = 35;
	
	/**
	 * The constructor initiates a new ScheduleCtr and builds up the layout.
	 */
	public SchedulePanel() {
		try {
			scheduleCtr = new ScheduleCtr();
		} 
		catch (Exception e) {
			//new StandardDialog().showErrorMessage("Check database connection");
		}
		super.getSplitPane().setRightComponent(new JPanel());
		super.getScrollPane().setBorder(new TitledBorder("Skemaer"));
		
		scheduleListModel = new DefaultListModel<>();
		scheduleList = new JList<>(scheduleListModel);
		initiateScheduleList();
		scheduleList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		super.getScrollPane().setViewportView(scheduleList);
	}
	
	/**
	 * Initializes the list of schedules and the listModel object by getting the data
	 * from the database. The boolean value false in the call means that associations
	 * of the Schedule table are not to be built.
	 */
	private void initiateScheduleList() {
		List<Schedule> scheduleList = null;
		try { 
			scheduleList = scheduleCtr.getAll(false);
			if (scheduleList.size() > 0){
				for(Schedule sch : scheduleList) {
					scheduleListModel.addElement(sch);
				}
			}
			else {
				new StandardDialog().objectNotFoundDialog("List af skemaer");
			}
		}
		catch (Exception e) { 
			new StandardDialog().showErrorMessage("Check database connection");
		}
	}
	
	
	@Override
	protected void doPrint() {
		// TODO Auto-generated method stub
		
	}
	
	/* (non-Javadoc)
	 * @see uiLayer.util.MainPanelTemplate#doEdit()
	 * 
	 * Opens the right panel of the SchedulePanel with the scheduleCtr and the chosen schedule
	 * object as parameters.
	 */
	@Override
	protected void doEdit() {
		Schedule schedule = scheduleList.getSelectedValue();
		if (schedule != null){
			super.getSplitPane().setRightComponent(new ScheduleEditPanel(scheduleCtr, schedule));
			//System.out.println(schedule.getCl());
		}
		else {
			new StandardDialog().showErrorMessage("Du skal vælge et skema for at kunne redigere.");
		}
		
	}
	
	/* (non-Javadoc)
	 * @see uiLayer.util.MainPanelTemplate#doNew()
	 * 
	 * Opens a dialog box to chose a class object the user will create a schedule for.
	 * Refreshes the list of schedules and the listModel from the database. The boolean value false 
	 * in the call means that associations of the schedule are not to be built.
	 */
	@Override
	protected void doNew() {
		List<Class> classes = null;
		try {
			classes = new ClassCtr().getAll(false);
		} catch (Exception e) {
			// db connection error
		}
		Object[] classArray = classes.toArray(new Object[classes.size()]);
		Class cl = null;
		cl = (Class)JOptionPane.showInputDialog(
		                    this,
		                    "Vil du oprette et nyt skema for: ",
		                    "Opret skema",
		                    JOptionPane.PLAIN_MESSAGE,
		                    null,
		                    classArray,
		                    "");
		if ((cl != null)) {
			try {
				scheduleCtr.createSchedule(cl.getClassName(), numberOfBlocks);
				Schedule schedule = scheduleCtr.findScheduleByName(cl.getClassName(), false);
				scheduleListModel.addElement(schedule);
				super.getSplitPane().setRightComponent(new ScheduleEditPanel(scheduleCtr, schedule));
			    return;
			}
			catch (AlreadyExistsException e) {
				new StandardDialog().showErrorMessage(e.getMessage());
			}
			catch (Exception e){
				//transaction error...
			}
		}
		
	}
	
	@Override
	protected void doDelete() {
		Schedule sch = this.scheduleList.getSelectedValue();
		if (sch != null) {
			int n = new StandardDialog().deleteDialog("Skema for ", sch.getCl().getClassName());
			if (n == JOptionPane.YES_OPTION){
				scheduleCtr.removeSchedule(sch);
				scheduleListModel.removeElement(sch);
			}
		}
		
	}

}
